<html>
<head><meta charset="utf-8"><title>Arc::clone(&amp;x) vs x.clone() · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html">Arc::clone(&amp;x) vs x.clone()</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="208499990"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208499990" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208499990">(Aug 30 2020 at 19:19)</a>:</h4>
<p>Is there some official (or unofficial) position on using <code>Arc::clone(&amp;x)</code> vs <code>x.clone()</code> ? I would like to ensure the std docs are consistent about that</p>



<a name="208500047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208500047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208500047">(Aug 30 2020 at 19:20)</a>:</h4>
<p>My understanding is that <code>Arc::clone(&amp;x)</code> is preferred so that it's clear that you're just cloning the reference and not the backing data.</p>



<a name="208500086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208500086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208500086">(Aug 30 2020 at 19:21)</a>:</h4>
<p>I just looked at the <code>Arc</code> docs and it looks like nearly every example uses <code>Arc::clone(&amp;x)</code>.</p>



<a name="208500145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208500145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208500145">(Aug 30 2020 at 19:23)</a>:</h4>
<p>Also see the <code>Rc</code> docs:</p>
<blockquote>
<p>The inherent methods of <code>Rc</code> are all associated functions, which means that you have to call them as e.g., <code>Rc::get_mut(&amp;mut value)</code> instead of <code>value.get_mut()</code>. This avoids conflicts with methods of the inner type <code>T</code>.</p>
</blockquote>
<p>And the <code>Rc</code> example for <code>Clone</code>:</p>
<div class="codehilite"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">rc</span>::<span class="n">Rc</span><span class="p">;</span><span class="w"></span>

<span class="kd">let</span><span class="w"> </span><span class="n">five</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Rc</span>::<span class="n">new</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span><span class="w"></span>

<span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Rc</span>::<span class="n">clone</span><span class="p">(</span><span class="o">&amp;</span><span class="n">five</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="208502619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208502619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DPC <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208502619">(Aug 30 2020 at 20:22)</a>:</h4>
<p>it's also mentioned somewhere on why all Arc methods are "free" (Arc::...)</p>



<a name="208502626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208502626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DPC <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208502626">(Aug 30 2020 at 20:22)</a>:</h4>
<p>not free but associative? or whatever you call them <span aria-label="grinning" class="emoji emoji-1f600" role="img" title="grinning">:grinning:</span></p>



<a name="208505880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208505880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208505880">(Aug 30 2020 at 21:51)</a>:</h4>
<p>It looks like it's "associated":</p>
<blockquote>
<p>Arc&lt;T&gt; automatically dereferences to T (via the Deref trait), so you can call T's methods on a value of type Arc&lt;T&gt;. To avoid name clashes with T's methods, the methods of Arc&lt;T&gt; itself are associated functions, called using function-like syntax:</p>
</blockquote>



<a name="208508125"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208508125" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208508125">(Aug 30 2020 at 22:55)</a>:</h4>
<p>In this case, I think you mean <a href="https://doc.rust-lang.org/stable/book/ch19-03-advanced-traits.html#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name">“fully qualified syntax”</a></p>



<a name="208508171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208508171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208508171">(Aug 30 2020 at 22:56)</a>:</h4>
<p>Since <code>Clone::clone</code> is a method (and all methods are associated functions)</p>



<a name="208512312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208512312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208512312">(Aug 31 2020 at 00:59)</a>:</h4>
<p>Perhaps that section of the docs should be changed to this then?</p>
<blockquote>
<p>To avoid name clashes with <code>T</code>'s methods, the methods of <code>Arc&lt;T&gt;</code> itself are associated functions, called using <a href="https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name">fully-qualified syntax</a>:</p>
</blockquote>



<a name="208512616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208512616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208512616">(Aug 31 2020 at 01:08)</a>:</h4>
<p>This doesn't really explain the situation with <code>x.clone()</code> though, since it is a trait implementation, not an associated function</p>



<a name="208512649"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208512649" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208512649">(Aug 31 2020 at 01:09)</a>:</h4>
<p>I don't think there is a reasonable argument that this can conflict with methods on the underlying type (unlike <code>Arc::get_mut()</code> for example), since it would be pathological to have a method called clone which is not a <code>Clone</code> implementation</p>



<a name="208513908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208513908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208513908">(Aug 31 2020 at 01:47)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> I've opened <a href="https://github.com/rust-lang/rust/issues/76138">#76138</a></p>



<a name="208514680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208514680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208514680">(Aug 31 2020 at 02:12)</a>:</h4>
<p>Its actually desired that it clash and that it supersedes the usual clone. That way you get the lighter clone.</p>



<a name="208514689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208514689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208514689">(Aug 31 2020 at 02:13)</a>:</h4>
<p>The reason I’ve always heard the explicit syntax recommended is so that you can easily visually audit that the light clone is being used</p>



<a name="208514739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208514739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208514739">(Aug 31 2020 at 02:14)</a>:</h4>
<p>IIRC, it's also possible to do <code>Vec::clone(...)</code> on an <code>&amp;Arc&lt;Vec&lt;T&gt;&gt;</code>, in case you want to have the expensive clone.</p>



<a name="208515640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208515640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208515640">(Aug 31 2020 at 02:43)</a>:</h4>
<p>Too bad the latter "feature" makes refactors more difficult.</p>



<a name="208515718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208515718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208515718">(Aug 31 2020 at 02:44)</a>:</h4>
<p>and so that's why you'd use <code>.clone()</code> for expensive things (so they automagically become cheap whenever possible) and <code>Arc::clone</code> so that they don’t accidentally become expensive</p>



<a name="208515727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208515727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208515727">(Aug 31 2020 at 02:45)</a>:</h4>
<p>but my remembering of the situation is similar to Jake's – its just a convention, not something that's strictly necessary.</p>



<a name="208519367"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208519367" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208519367">(Aug 31 2020 at 04:21)</a>:</h4>
<p>Static associated functions. For <code>Arc</code> and <code>Box</code> and other smart pointer friends, it's to not shadow the methods on their inner types.</p>



<a name="208554614"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208554614" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208554614">(Aug 31 2020 at 13:15)</a>:</h4>
<p>Rust doesn't use the term "static" for them (that's more a legacy of other languages). All methods are also associated functions, but not all associated functions are methods. However, most people treat methods and associated functions as disjoint sets.</p>
<p>Clone is a method (and thus also an associated function). It <strong>does</strong> shadow the inner method, so that's not why it's encouraged to be called like this.</p>



<a name="208556202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208556202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208556202">(Aug 31 2020 at 13:30)</a>:</h4>
<p>I opened a PR to update the docs with x.clone() -&gt; (Arc|Rc)::clone(&amp;x) whenever possible to be more consistent with what’s used in the Arc/Rc docs</p>



<a name="208556783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arc%3A%3Aclone%28%26x%29%20vs%20x.clone%28%29/near/208556783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arc.3A.3Aclone(.26x).20vs.20x.2Eclone().html#208556783">(Aug 31 2020 at 13:36)</a>:</h4>
<p><a href="https://github.com/rust-lang/api-guidelines/issues/200">https://github.com/rust-lang/api-guidelines/issues/200</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>